<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      :label-position="labelPosition"
      label-width="100px"
    >
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="培训名称">
            <el-input v-model="form.name" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训开始时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.startDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训结束时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.endDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训学时">
            <el-input v-model="form.period" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训方式">
            <el-input v-model="form.form" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训内容">
            <el-input v-model="form.content" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训成绩(分)" prop="grade">
            <el-input maxlength="8" v-model.number="form.grade" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="培训费用(元)" prop="cost">
            <el-input maxlength="8" v-model.number="form.cost" :style="inputWidth" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="备注">
            <el-input type="textarea" :rows="2" v-model="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增'}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserTrain, createUserTrain } from '@/api/archives'
import { openNotification, getSession, validateNum } from '@/utils/util'

export default {
  data() {
    return {
      visible: false,
      form: {},
      labelPosition: 'right',
      inputWidth: {
        width: '100%'
      },
      rules: {
        grade: validateNum,
        cost: validateNum
      },
      isEdit: false,
      userRow: null
    }
  },
  created() {
    if (this.$route.query.userRow) {
      this.userRow = JSON.parse(this.$route.query.userRow)
    } else {
      this.userRow = getSession('userRow')
    }
  },
  methods: {
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          this.$set(this.form, 'userId', this.userRow.userId)
          if (this.isEdit) {
            res = await updateUserTrain(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserTrain(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    },
    edit(bool, row) {
      console.log('row', row)
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, row)
    }
  }
}
</script>
